﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using SurfaceInteraction.MetaData;
using SurfaceInteraction.MetaData.ServiceContracts;

namespace SurfaceInteraction.Services.Implementation
{
    [ServiceBehavior(Namespace = Schemas.SURFACEINTERACTION_SERVICE_SCHEMA, InstanceContextMode = InstanceContextMode.Single)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class RemoteDesktopFrameService : IRemoteDesktop
    {
        private static List<SurfaceFrame> __desktopFrames;

        static RemoteDesktopFrameService()
        {
            __desktopFrames = new List<SurfaceFrame>();
        }

        public void SetFrame(SurfaceFrame frame)
        {
            lock (__desktopFrames)
            {
                if (__desktopFrames.Any(f => f.Id == frame.Id))
                {
                    SurfaceFrame fr = __desktopFrames.Single(f => f.Id == frame.Id);
                    fr.FrameDate = frame.FrameDate;
                    fr.Frame = frame.Frame;
                }
                else
                {
                    __desktopFrames.Add(frame);
                }
            }
        }

        public SurfaceFrame GetFrame(int id)
        {
            SurfaceFrame retVal = null;
            if (__desktopFrames.Any(f => f.Id == id))
            {
                retVal = __desktopFrames.Single(f => f.Id == id);

            }

            return retVal;
        }
    }
}
